<template>
  <header class="header clearfix">
    <div class="top">
      <div class="w clearfix">
        <div class="left">
          <h4>One Piece 小海米一枚</h4>
        </div>
        <div v-if="uname" class="right" @click="person">
          欢迎，小海迷{{ uname }}

          <i class="el-icon-arrow-down"></i> &nbsp;&nbsp;&nbsp;
          <div class="person" v-show="flag">
            <ul>
              <router-link to="/user"
                ><li>
                  <i class="el-icon-user-solid"></i> 个人资料
                </li></router-link
              >
              <router-link to="/order">
                <li><i class="el-icon-s-finance"></i>我的订单</li>
              </router-link>
              <li @click="logout">
                <i class="el-icon-switch-button"></i> 安全退出
              </li>
            </ul>
          </div>
          <!-- <span @click="logout" style="cursor: pointer">注销</span> -->
        </div>
        <div class="right clearfix" v-else>
          <!-- <a href="javascript:;">登录</a>&nbsp;&nbsp; -->
          <router-link to="/login">登录</router-link>&nbsp;&nbsp;
          <router-link to="/register">注册</router-link>
        </div>
        <div class="right cart" v-show="uname" @click="cart">
          <i class="el-icon-shopping-cart-2"></i>
          <p v-show="counts">{{ counts }}</p>
        </div>
      </div>
    </div>
    <div class="buttom">
      <div class="w clearfix">
        <div class="logo">
          <router-link to="/"
            ><img src="../assets/logo.webp" alt=""
          /></router-link>
        </div>
        <ul @click="active">
          <router-link to="/"
            ><li class="active1">
              首页<i class="active1">/ HOME</i>
            </li></router-link
          >
          <li>商城<i>/ SHOP</i></li>
          <li>博客<i>/ BLOG</i></li>
          <li>关于<i>/ ABOUT</i></li>
          <li>联系<i>/ CONTACT</i></li>
        </ul>

        <div class="search">
          <input
            type="text"
            placeholder="日常用品/家具/家纺"
            @keyup.13="search"
            v-model="kw"
          /><a @click="search"></a>
        </div>
      </div>
    </div>
  </header>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      kw: "",
      flag: false,
    };
  },
  methods: {
    ...mapMutations(["setUname"]),
    person() {
      if (this.flag) {
        this.flag = false;
      } else {
        this.flag = true;
      }
    },
    cart() {
      this.$router.push("/cart");
    },
    active(e) {
      if (e.target.nodeName == "LI") {
        var lis = document.querySelectorAll("li");
        var is = document.querySelectorAll(".buttom ul i");
        for (var i of is) {
          i.className = "";
        }
        for (var li of lis) {
          li.className = "";
          // li.children[0] = "";
        }
        e.target.className = "active1 ";
        // console.log(e.target.children);
        e.target.children[0].className = "active1";
      }
    },
    logout() {
      this.setUname("");
      sessionStorage.setItem("uname", "");
      sessionStorage.setItem("uid", "");
      sessionStorage.setItem("counts", "");
      this.$router.push("/");
    },
    search() {
      // console.log(`搜索的商品是${this.kw}......`);
      // 判断如果kw不为空才查询
      if (this.kw) {
        this.$router.push(`/product/${this.kw}`);
      } else {
        this.$router.push(`/product/日常用品`);
      }
    },
  },
  created() {
    this.kw = this.$route.params.kw;
    // console.log(this.images);
    // console.log(this.$store.state.images);
  },
  // mounted() {
  //   console.log(this.counts);
  // },
  watch: {
    $route() {
      this.kw = this.$route.params.kw;
    },
  },
  computed: {
    ...mapState(["uname", "counts"]),
  },
};
</script>
<style >
.header .icon {
  width: 20px;
  height: 20px;
}
.header a {
  outline: none;
}
.clearfix::after {
  content: "";
  display: block;
  height: 0;
  clear: both;
}
.header {
  background-color: #f6f6f6;
}

.header .top {
  height: 40px;
  font-size: 12px;
  color: #b0b0b0;
  line-height: 40px;
  background-color: #333;
}
.w {
  width: 1200px;
  margin: 0 auto;
  position: relative;
}
.header .top .left {
  float: left;
}
.header .top .right {
  float: right;
  position: relative;
  cursor: pointer;
}
.header .top .right .person {
  position: absolute;
  top: 40px;
  left: 28px;
  width: 89px;
  height: 100px;
  z-index: 10;
  background-color: #fff;
}
.header .top .right .person li {
  font-size: 13px;
  width: 80px;
  height: 33px;
  color: #666;
  cursor: pointer;
}
.header .top .right .person li i {
  font-size: 16px;

  margin: 3px 5px 0 3px;
  color: #333;
}
.header .top .right a {
  color: #b0b0b0;
}
.header .buttom {
  position: relative;
  height: 86px;
  line-height: 86px;
  background-color: #f6f6f6;
}
.header .buttom .logo {
  float: left;
}
.header .buttom .logo img {
  width: 150px;
  height: 34px;
}
.header .buttom ul {
  float: left;
  margin-left: 50px;
}
.header .buttom ul li {
  float: left;
  font-size: 16px;
  color: #333333;
  margin-left: 30px;
  cursor: pointer;
}
i {
  font-size: 12px;
  color: #a3a3a3;
}
.active1 {
  color: #d19f4f !important;
}
.header .buttom ul li:hover {
  color: #d19f4f;
}
.header .buttom ul li:hover i {
  color: #d19f4f;
}
.header .buttom .search {
  float: right;
  width: 320px;
  height: 44px;
  margin-top: 21px;
}
.header .buttom .search input {
  float: left;
  width: 245px;
  height: 40px;
  font-size: 12px;
  border: 1px solid #ccc;
}
.header .buttom .search a {
  float: left;

  width: 45px;
  height: 42px;
  border: 1px solid #ccc;
  border-left: 0;
  background: url("../assets/serach.png");
  cursor: pointer;
}
.header .top .cart {
  position: absolute;
  top: 0;
  right: -20px;
}
.header .top .cart i {
  font-size: 30px;
  line-height: 42px;
}
.header .top .cart p {
  position: absolute;
  top: 4px;
  min-width: 10px;
  right: -10px;
  height: 15px;
  line-height: 15px;
  text-align: center;
  background-color: #fff;
  border-radius: 5px 5px 5px 0;
}
</style>